Repository navigation
Add actionable remediation approaches and patterns for CA2219 (Fixes #48230) - #56653
Open
Manish-1kumar wants to merge 1 commit into
Open
Manish-1kumar wants to merge 1 commit into
Manish-1kumar wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The cleanup example can still mask the primary exception if logging throws and can falsely report success after required cleanup fails.
3 open issues
What changed in this PR
Expands the CA2219 documentation with practical approaches to cleanup failures, addressing the guidance gap raised in #48230.
Changes:
- Adds a catch-and-log example for cleanup inside
finally. - Explains exception-filter behavior and when to use
Environment.FailFast. - Clarifies exception masking and suppression guidance, and refreshes article metadata.
| File | Description |
|---|---|
docs/fundamentals/code-analysis/quality-rules/ca2219.md |
Adds cleanup-failure patterns and clarifies CA2219 remediation guidance. |
Review effort: Balanced
|
|
||
| ### Catch and log cleanup exceptions inside `finally` | ||
|
|
||
| If cleanup operations—such as disposing unmanaged resources, closing streams, or calling native P/Invoke APIs—can fail, wrap those operations in an internal `try/catch` block inside the `finally` clause: |
| { | ||
| // Log the cleanup exception without rethrowing, | ||
| // preventing it from masking any in-flight exception. | ||
| logger.LogError(ex, "Failed to clean up resource."); |
Comment on lines
+47
to
+48
| ```csharp | ||
| try |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Description
Resolves #48230.
In \docs/fundamentals/code-analysis/quality-rules/ca2219.md\ (CA2219: Do not raise exceptions in exception clauses), replaces the brief prohibition with concrete guidance and alternative patterns for handling cleanup operations that can fail.
Changes Included